$('#bind-bg').height($(window).height());
var sendSuccess = function(resp){
    if(resp.code==0){
        var countdown = 59;
        $('#v-code').text(countdown+"s");
        var timer = setInterval (function(){
            countdown --;
            $('#v-code').text(countdown+"s");
            $('#v-code').val("")
            if(countdown <=0){
                clearInterval(timer);
                $('#v-code').text("获取验证码");
                $('#v-code').val("获取验证码")
            }
        },1000);
        $('#loginFailInfo').text("验证码已发送");
    }else{
        alert(resp.message);
    }

}
var sendFail = function(resp){
    alert(resp.message);
}
var countcode = function(){
    console.log(1111);
    var phone = $('#username').val();
    var phReg=/^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if(!phReg.test(phone)){
        alert('请输入正确的手机号');
        return;
    }
    if($('#v-code').val() == "获取验证码"){
        wfHttp.post("/verifyCode/"+phone+"?token="+localStorage.token,undefined,{succ: sendSuccess,fail:sendFail})
    }
//    请求验证码
}
var getTokenFromCurrentUrl = function(){
    var token = GetQueryString("token");
    var openId =GetQueryString("openId");
    var portrait =GetQueryString("portrait");
    if(token!=null){
        localStorage.token = token;
    }
    if(portrait!=null){
        localStorage.portrait = portrait;
        $('#bind-avatar').attr('src',decodeURIComponent(portrait));
    }
    if(openId!=null){
        localStorage.openId = openId;
    }
    console.log(localStorage.token);
    console.log(localStorage.openId);

}
getTokenFromCurrentUrl();

var bindSuccess = function(resp){
    if(resp.code == 0){
        localStorage.token = resp.message;
        window.location.href = "usercenter.html"
    }else{
        $('#loginFailInfo').text(JSON.stringify(resp.message));
    }
    //window.location.href = "usercenter.html"
}
var bindFail = function(resp){
    $('#loginFailInfo').text('服务器繁忙,请稍候再试');
}
var doBind = function(){
    var username=$('#username').val();
    var pwd=$('#pwd').val();
    var code = $('#code').val();
    if(username==''||username==undefined){
        $('#loginFailInfo').text('请输入手机号或邮箱');
        return ;
    }
    var phReg=/^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if(phReg.test(username)){
        if(code==''||code==undefined){
            $('#loginFailInfo').text('请输入验证码');
            return ;
        }
    }

    if(pwd==''||pwd==undefined){
        $('#loginFailInfo').text('请输入密码');
        return ;
    }
    console.log(username,pwd);
    var postData={
        account:'pkcrm',
        username:username,
        password:hex_md5(pwd),
        openId:localStorage.openId,
        verifyCode:code
    };
    var url = "/wechat/member/bind?username="+postData.username+"&password="
        +postData.password+"&openId="+postData.openId
        +"&verifyCode="+postData.verifyCode
    if(postData.verifyCode == ''){
        url = "/wechat/member/bind?username="+postData.username+"&password="
        +postData.password+"&openId="+postData.openId
    }
    wfHttp.post(url,undefined,{succ: bindSuccess,fail:bindFail})
};
